Completed
Push — master ( 9f1408...83ea15 )
by Nicolaas
03:49
created

UpdatePrices.init   A

Complexity

Conditions 1
Paths 4

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 4
nop 0
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 21 4
A 0 17 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
(function($) {
2
	$.entwine('ecommerceUpdatePricing', function($) {
3
		$('#Root_Pricing').entwine({
4
			onmatch : function() {
5
				UpdatePrices.init();
0 ignored issues
show
Bug introduced by
The variable UpdatePrices seems to be never declared. If this is a global, consider adding a /** global: UpdatePrices */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
			}
7
		});
8
9
	});
10
11
}(jQuery));
12
13
UpdatePrices = {
0 ignored issues
show
Bug introduced by
The variable UpdatePrices seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.UpdatePrices.
Loading history...
14
15
	parentElement: null,
16
17
	init: function() {
18
		this.parentElement = jQuery('#Root_Pricing')
19
		var countryCurrencies = jQuery.parseJSON(jQuery(UpdatePrices.parentElement).find('[name=CountryCurrencies]').val());
0 ignored issues
show
Bug introduced by
The variable UpdatePrices seems to be never declared. If this is a global, consider adding a /** global: UpdatePrices */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
20
		var fromField = jQuery(UpdatePrices.parentElement).find('#From select');
21
		var toField = jQuery(UpdatePrices.parentElement).find('#To');
22
23
		jQuery(fromField).change(
24
			function() {
25
				var code = jQuery(this).val();
26
				var countries = new Array();
0 ignored issues
show
Coding Style Best Practice introduced by
Using the Array constructor is generally discouraged. Consider using an array literal instead.
Loading history...
27
				if(code in countryCurrencies) {
28
					var currency = countryCurrencies[code];
29
					jQuery.each(countryCurrencies, function(countryCode, countryCurrency) {
30
						if(countryCurrency == currency && countryCode != code) {
31
							countries.push(countryCode);
32
						}
33
					});
34
				}
35
				jQuery(toField).find('li').hide();
36
				jQuery(toField).find(':checkbox').attr('checked', false);
37
				for(i = 0; i < countries.length; i++) {
0 ignored issues
show
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
38
					jQuery(toField).find('li.val' + countries[i]).show();
39
				}
40
			}
41
		);
42
		jQuery(fromField).change();
43
44
		jQuery(this.parentElement).find('#UpdatePriceLink a').click(
45
			function(event) {
46
				var fromCountry = jQuery(fromField).val();
47
				var toCountries = jQuery(toField).find(':checkbox:checked');
48
				if(fromCountry && jQuery(toCountries).length > 0) {
49
					var confirmation = confirm('Are you sure that you want to copy those prices over ?');
50
					if(confirmation) {
51
						toCountries = jQuery.map(toCountries, function(element) {
52
							return jQuery(element).attr('value');
53
	 					});
54
						toCountries.join(',');
55
						var link = jQuery(this).attr('href');
56
						link += '&' + jQuery(fromField).attr('name') + '=' + jQuery(fromField).val();
57
						link += '&' + jQuery(toField).attr('id') + '=' + toCountries;
58
						alert(link);
59
						jQuery(this).attr('href', link);
60
						return true;
61
					}
62
				}
63
				event.preventDefault();
64
				return false;
65
			}
66
		);
67
	}
68
}
69
70